package com.example.lenovo.tianqi.fragment;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.example.lenovo.tianqi.Bean.NewWeatherBean;
import com.example.lenovo.tianqi.R;
import com.example.lenovo.tianqi.adapter.MultiCityAdapter;
import com.example.lenovo.tianqi.net.CallBack.IError;
import com.example.lenovo.tianqi.net.CallBack.IFailure;
import com.example.lenovo.tianqi.net.CallBack.ISuccess;
import com.example.lenovo.tianqi.net.RestClient;
import com.example.lenovo.tianqi.util.MySharedpreference;
import com.example.lenovo.tianqi.util.NetState;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lenovo on 2018/1/9.
 */

public class ManagerFragment extends Fragment {
    @BindView(R.id.manager_recyclerview)
    RecyclerView managerRecyclerview;
    @BindView(R.id.manager_swiprefresh)
    SwipeRefreshLayout managerSwiprefresh;
    Unbinder unbinder;
    @BindView(R.id.empty)
    LinearLayout empty;
    @BindView(R.id.manager_layout)
    RelativeLayout managerLayout;
    @BindView(R.id.mymanager_text)
    TextView mymanagerText;
    SharedPreferences.Editor editor;
    private View view;
    private NewWeatherBean mNewWeatherBean;
    private MultiCityAdapter adapter;
    protected  boolean mIsCreatView=false;
    private ArrayList<NewWeatherBean> data;
    private int OldMultiCityNum;  //记录更新UI前的城市个数
    private int MultiCityNum;
    private int TestNum;

    private final int NO_MORE_CITIS=1;
    private final int HAVE_MORE_CITIS=2;
    private final int NO_INTERNET=3;
    private final int ADD_CITY=4;
    private final int UPDATA=5;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case NO_MORE_CITIS://没有城市
                    empty.setVisibility(View.VISIBLE);
                    break;
                case HAVE_MORE_CITIS://有多的城市
                    MultiCityNum--;
                if(MultiCityNum==0)
                {
                    adapter=new MultiCityAdapter(R.layout.item_multi_city,data,getContext());
                    managerRecyclerview.setAdapter(adapter);
                    adapter.openLoadAnimation(SetAnim(MySharedpreference.preferences.getInt("Multi_anim",0)));
                    adapter.isFirstOnly(false);
                    AddSwipeListener();
                }
                break;
                case NO_INTERNET://没有网络
                    Toast.makeText(getContext(), "没有网络", Toast.LENGTH_SHORT).show();
                    break;
                case ADD_CITY://添加城市
                    if(OldMultiCityNum==0)
                    {
                        adapter=new MultiCityAdapter(R.layout.item_multi_city,data,getContext());
                        managerRecyclerview.setAdapter(adapter);
                        adapter.openLoadAnimation(SetAnim(MySharedpreference.preferences.getInt("Multi_anim",0)));
                        adapter.isFirstOnly(false);
                        empty.setVisibility(View.GONE);
                        AddSwipeListener();
                    }
                    else adapter.notifyDataSetChanged();
                break;
                case UPDATA://刷新数据
                    MultiCityNum--;
                if (MultiCityNum==0)
                {
                    adapter.notifyDataSetChanged();
                    managerSwiprefresh.setRefreshing(false);
                }
                break;
            }
        }
    };

    private void AddSwipeListener() {
        ItemDragAndSwipeCallback itemDragAndSwipeCallback=new ItemDragAndSwipeCallback(adapter);
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(managerRecyclerview);
        adapter.enableSwipeItem();
        adapter.setOnItemSwipeListener(new OnItemSwipeListener() {
            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                int CityNum=MySharedpreference.preferences.getInt("MultiCityNum",0);
                CityNum--;
                editor.putInt("MultiCityNum",CityNum);
                editor.remove("MultiCityWeather"+(adapter.getItemId(pos)+1));//在editor里清除删除指定数据
                Log.d("gggg",""+(adapter.getItemId(pos)+1));
                editor.remove("MultiCity"+(adapter.getItemId(pos)+1));
                editor.commit();
                if(CityNum==0)
                    empty.setVisibility(View.VISIBLE);//显示没有城市
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

            }
        });
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_mymanager, container, false);
            ButterKnife.bind(this, view);
            initView();
            data=new ArrayList<>();
            editor=MySharedpreference.getInstance(getContext());
            managerRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
            MultiCityNum = MySharedpreference.preferences.getInt("MultiCityNum",0);////////////

            if(MultiCityNum==0)//没有更多城市
            {
                handler.sendEmptyMessage(NO_MORE_CITIS);
            }
            else if(JugeData())
            {
                for(int i=1;i<=MultiCityNum;i++)/////////
                {
                    String weatherInfo=MySharedpreference.preferences.getString("MultiCityWeather"+i,null);
                    Gson g=new Gson();
                    data.add(g.fromJson(weatherInfo,NewWeatherBean.class));
                    handler.sendEmptyMessage(HAVE_MORE_CITIS);
                }
            }
            else
            {
                if(NetState.isNetworkAvailable(getContext()))
                {
                    for(int i=1;i<=MultiCityNum;i++)
                    {
                        String MultiCityName = MySharedpreference.preferences.getString("MultiCity"+i,null);
                        initWeatherInfo(MultiCityName,HAVE_MORE_CITIS);
                    }

                }
                else {
                    handler.sendEmptyMessage(NO_INTERNET);//无网络
                }
            }
        }

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private boolean JugeData() {
        SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = mDateFormat .format(new Date());
        String SharedDate = MySharedpreference.preferences.getString("Date_Multi",null);
        if(SharedDate==null||!date.equals(SharedDate))
        {
            editor.putString("Date_Multi",date);
            editor.commit();
            return false;
        }
        else if(date.equals(SharedDate))
        {
            return true;
        }
        return false;
    }

    private void initView() {
        managerSwiprefresh.setColorSchemeColors(Color.BLACK,Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED);
        managerSwiprefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(NetState.isNetworkAvailable(getContext()))
                {
                    data.clear();//清空数据
                    MultiCityNum=MySharedpreference.preferences.getInt("MultiCityNum",0);
                    if(MultiCityNum==0)
                    {
                        handler.sendEmptyMessage(NO_MORE_CITIS);
                        managerSwiprefresh.setRefreshing(false);
                    }
                    else {
                        for(int i=1;i<=MultiCityNum;i++)
                        {
                            String MultiCityName=MySharedpreference.preferences.getString("MultiCity"+i,null);
                            initWeatherInfo(MultiCityName,UPDATA);
                        }
                    }
                }
                else
                {
                    Toast.makeText(getContext(), "请检查网络", Toast.LENGTH_SHORT).show();
                    managerSwiprefresh.setRefreshing(false);
                }
            }
        });
    }

    private void initWeatherInfo(String multiCityName,final int updata) {
        if(multiCityName!=null)
        {
            RestClient.builder()
                    .url("https://free-api.heweather.com/s6/weather?parameters")
                    .params("location",multiCityName)
                    .params("key","b844972b249244019f2afb19e1f3c889")
                    .context(getContext())
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            Log.d("cc","MultiCityWeather"+response);
                            Gson g = new Gson();
                            mNewWeatherBean = g.fromJson(response,NewWeatherBean.class);
                            if(mNewWeatherBean.getHeWeather6().get(0).getStatus().equals("ok"))
                            {
                                data.add(mNewWeatherBean);
                                editor.putString("MultiCityWeather"+MultiCityNum,response);
                                editor.commit();
                                handler.sendEmptyMessage(updata);
                            }
                            else
                            {
                                int CityNum  = MySharedpreference.preferences.getInt("MultiCityNum",0);
                                CityNum--;
                                editor.putInt("MultiCityNum",CityNum);
                                editor.remove("MultiCityWeather"+MultiCityNum);
                                editor.remove("MultiCity"+MultiCityNum);
                                editor.commit();
                                if(CityNum==0) empty.setVisibility(View.VISIBLE);//显示没有城市
                            }

                        }
                    })
                    .failure(new IFailure() {
                        @Override
                        public void onFailure() {
                            Toast.makeText(getContext(),"请检查网络",Toast.LENGTH_SHORT).show();
                            managerSwiprefresh.setRefreshing(false);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {
                            Toast.makeText(getContext(),"加载错误code"+code,Toast.LENGTH_SHORT).show();
                            managerSwiprefresh.setRefreshing(false);
                        }
                    })
                    .build()
                    .post();
        }
    }
    public void UpDataUi()
    {
        MultiCityNum = MySharedpreference.preferences.getInt("MultiCityNum",0);
        OldMultiCityNum = MultiCityNum - 1;
        if(NetState.isNetworkAvailable(getContext()))
        {
            if(MultiCityNum!=0)
            {
                String MultiCityName = MySharedpreference.preferences.getString("MultiCity"+MultiCityNum,null);
                initWeatherInfo(MultiCityName,ADD_CITY);
            }
            else handler.sendEmptyMessage(NO_MORE_CITIS);//没有更多城市
        }
        else handler.sendEmptyMessage(NO_INTERNET);//没有网络
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private int SetAnim(int multi_anim) {
        switch (multi_anim)
        {
            case 0:
                return 0x00000001;
            case 1:
                return 0x00000002;
            case 2:
                return 0x00000003;
            case 3:
                return 0x00000004;
            case 4:
                return 0x00000005;
        }
        return 0x00000001;
    }

}
